package br.com.officium.managedbean.cadastro;

import br.com.officium.entidade.cadastro.Area;
import br.com.officium.sessionbean.AreaBsn;
import br.com.officium.util.BeanUtil;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Ivo
 */
@ManagedBean(name = "areaBean")
@ViewScoped
public class AreaBean extends CadastroAbstractBean {

    @EJB
    private AreaBsn areaBsn;
    private Area area;
    private List<Area> areas;

    public AreaBean() {
        area = new Area();
    }

    @Override
    public String listar() {
        setCurrentState(LISTAR_STATE);
        areas = areaBsn.findAll();
        return "/cadastro/area.jsf";
    }

    @Override
    public void cadastrar() {

        try {
            areaBsn.create(area);
            clear();
            BeanUtil.addInfo("Área criada com sucesso.");
            areas = areaBsn.findAll();
        } catch (Exception e) {
            BeanUtil.addError(e.getMessage());
        }

    }

    @Override
    public void editar() {

        try {
            areaBsn.edit(area);
            clear();
            setCurrentState(LISTAR_STATE);
            BeanUtil.addInfo("Área alterada com sucesso.");
        } catch (Exception e) {
            BeanUtil.addError(e.getMessage());
        }

    }

    @Override
    public void excluir() {

        try {
            areaBsn.remove(area);
            BeanUtil.addInfo("Área removida com sucesso.");
            areas = areaBsn.findAll();
        } catch (Exception e) {
            BeanUtil.addError(e.getMessage());
        }

    }

    @Override
    public boolean isPaginator() {
        return areaBsn.count() > getRowNumber();
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public List<Area> getAreas() {
        return areas;
    }

    public void setAreas(List<Area> areas) {
        this.areas = areas;
    }

    private void clear() {
        this.area = new Area();
        super.cleanSubmittedValues(panelForm);
    }
}
